package com.cos.lession72.web.controller;

import com.cos.lession72.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.rmi.log.LogInputStream;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/9/29 9:19
 * @description:
 */
@Controller
@RequestMapping("/con")
public class MyController {

    @RequestMapping("/t1")
    public String t1(@RequestParam(required = false, value = "ename", defaultValue = "1") Integer name) {
        System.out.println("t1");
        return "index";
    }

    @RequestMapping("/t2/{name}")
    public String t2(@PathVariable("name") String name) {
        System.out.println("t1" + name);
        return "index";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/29 9:30
     * @description: 响应数据的方式1
     */
    @RequestMapping("/t3")
    public String t3(HttpServletRequest request) {
        request.setAttribute("msg", "httpServletRequest 对象 存储数据");
        return "index";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/29 9:33
     * @description: 方式2:map进行传参
     */
    @RequestMapping("/t4")
    public String t4(Map<String, Object> maps) {
        maps.put("msg", "使用map进行数据传参");
        return "index";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/29 9:33
     * @description: 方式3:使用model进行传参
     */
    @RequestMapping("/t5")
    public String t5(Model model) {
        model.addAttribute("msg", "使用model传参");
        return "index";
    }

    @RequestMapping("/t6")
    public ModelAndView t6() {
        ModelAndView mv = new ModelAndView();
        //设置 model数据模型
        mv.addObject("msg", "使用modelAndView进行传参");
        //设置逻辑视图名
        mv.setViewName("index");
        return mv;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/29 10:33
     * @description: 设置 @ResponseBody 方法返回值为String
     * 就是直接写出字符串 作用响应
     */
    @RequestMapping("/t7")
    @ResponseBody
    public String t7() {
        return "ok";
    }

    //    @RequestMapping(value = "/t8",produces = "application/json;charset=utf-8")
    @RequestMapping(value = "/t8")
    @ResponseBody
    public String t8() throws JsonProcessingException {
        User u = new User(1, 18, "张三", "妖", 123.0, new Date());
        ObjectMapper om = new ObjectMapper();
        String string = om.writeValueAsString(u);
        return string;
    }

    @RequestMapping(value = "/t9")
    @ResponseBody
    public User t9() throws JsonProcessingException {
        User u = new User(1, 18, "李四", "女", 123.0, new Date());
        // springmvc 会把返回值自己转换为JSon字符串在写出
        return u;
    }

    @RequestMapping(value = "/t10", method = RequestMethod.GET)
    @ResponseBody
    public List<User> t10() throws JsonProcessingException {
        User u = new User(1, 18, "李四", "女", 123.0, new Date());
        // springmvc 会把返回值自己转换为JSon字符串在写出
        List<User> ul = new ArrayList<>();
        ul.add(u);
        ul.add(new User(1, 18, null, "女", 123.0, new Date()));
        ul.add(new User(1, 18, null, "女", 123.0, new Date()));
        ul.add(new User(1, 18, null, "女", 123.0, new Date()));
        return ul;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/9/29 14:21
     * @description:
     */
//    @RequestMapping(value = "/t11",consumes = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/t11", method = RequestMethod.POST)
    @ResponseBody
    public String t11(@RequestBody User user) throws JsonProcessingException {
        System.out.println(user);
        return "ok";
    }

    @RequestMapping(value = "/t12", method = RequestMethod.GET)
    public String t12(Integer num) {
        if (num == 1) {
            String a = null;
            System.out.println(a.equals(""));
        } else if (num == 2) {
            int[] a = new int[0];
            a[1]=1;
        }else{
            System.out.println(10/0);
        }
        return "ok";
    }


}
